/*
 * author      : 吴中勤
 * date        : 2019/3/12
 * description : TODO
 */

package org.wzq.learn;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {


    /*
    @Autowired
    private final Tracer tracer;

    public GlobalExceptionHandler(Tracer tracer) {
        this.tracer = tracer;
    }
    */

    @Autowired
    private SentryService sentryService;


    private static final int SYSTEM_ERROR_CODE = 500;

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseEntity<?> handler(HttpServletResponse response, HttpServletRequest request, Exception ex) {
        HttpResponsePayload payload;

        // String xRequestId = request.getHeader("x-request-id");
        String xTraceId = response.getHeader("x-trace-id");

        sentryService.sendAPIError(xTraceId, ex);

//        if (xRequestId != null) {
//            if (logger.isInfoEnabled()) logger.info("需要上报此异常, x-request-id=" + xRequestId);
//            // TODO 上报到 sentry
//            sentryService.sendAPIError(xRequestId);
//        } else {
//            sentryService.sendException(ex);
//        }

        if (ex instanceof ResponseStatusException) {
            ResponseStatusException ex2 = (ResponseStatusException) ex;
            int code = ex2.getStatus().value();
            payload = HttpResponseAssembler.unhappy(code, ex.getMessage());
            return new ResponseEntity<>(payload, HttpStatus.valueOf(code));
        } else {
            payload = HttpResponseAssembler.unhappy(SYSTEM_ERROR_CODE, ex.getMessage());
            return new ResponseEntity<>(payload, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

//    @ExceptionHandler(value = ResponseStatusException.class)
//    public void responseExceptionHandler(HttpServletResponse response, HttpServletRequest request, ResponseStatusException ex) throws IOException {
//        int code = ex.getStatus().value();
//        if (logger.isInfoEnabled()) logger.info("handleIllegalArgumentException, ex.code" + code);
//
//        // 有价值的尝试
//        // 此时 设置 sampler.always 无效，估计是时机已经晚了
//        if (code > HttpStatus.BAD_REQUEST.value()) {
//            if (logger.isInfoEnabled()) logger.info("should collect this span");
//            tracer.withSampler(Sampler.ALWAYS_SAMPLE);
//        }
//    }
//
//    @ExceptionHandler(value = Exception.class)
//    public void allOtherExceptionHandler(HttpServletResponse response, HttpServletRequest request) throws IOException {
//        if (logger.isInfoEnabled()) logger.info("allOtherExceptionHandler");
//        tracer.withSampler(Sampler.ALWAYS_SAMPLE);
//    }
}

